#include <stdlib.h>
#include "bounded_string.h"

int search_string (const BOUNDED_STRING *strings, size_t nstrings, compare_function compare, const char *string, size_t len)
{
	int i, j, pivot;
	int ret;

	i = 0;
	j = nstrings - 1;

	while (i <= j) {
		pivot = (i + j) / 2;
		ret = compare (string, strings[pivot].string, len);
		if (ret < 0) {
			j = pivot - 1;
		} else if (ret == 0) {
			if (len < strings[pivot].len) {
				j = pivot - 1;
			} else if (len == strings[pivot].len) {
				return pivot;
			} else {
				i = pivot + 1;
			}
		} else {
			i = pivot + 1;
		}
	}

	return -1;
}
